import { IConditionQueryFeatures, Service } from "vjmap";
import { cacheStorage } from "./cache";
import vjmap3d from "vjmap3d";

//从后台查询图中的数据（可缓存至本地)
export const queryFeatures = async (svc: Service | vjmap3d.Service, param: IConditionQueryFeatures  | vjmap3d.IConditionQueryFeatures, useCache?: boolean) => {
    let curParam = svc.currentMapParam();
    let key = `${curParam?.mapid}_${curParam?.version}_${svc.getCurWorkspaceName()}_${svc.strMd5(JSON.stringify(param))}`;
    if (useCache) {
        const data = await cacheStorage.getValueByKey(key, true);
        if (data) return data; // 如果缓存中有数据
    }
    let query = await svc.conditionQueryFeature(param);
    let results = query.result;
    if (!results) return query;
    if (useCache) {
        //  保存结果至缓存中
        await cacheStorage.setValueByKey(key, query, true);
    }
    return query;
}


